package com.mashibing.tank.net;

import com.mashibing.tank.*;
import lombok.Data;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.UUID;

/**
 * @description:
 * @author: 余希瑶
 * @date: 2021年06月27日 16:41
 * @version:1.0
 */
@Data
public class TankJoinMsg {

    private int x;
    private int y;
    private Dir dir;
    private boolean moving;
    private Group group;

    //self `s id
    private UUID id;

    public TankJoinMsg() {
    }

    public TankJoinMsg(Player p) {
        this.x = p.getX();
        this.y = p.getY();
        this.dir = p.getDir();
        this.moving = p.isMoving();
        this.group = p.getGroup();
        this.id = p.getId();
    }

    public byte[] toBytes() {

        ByteArrayOutputStream baos = null;
        DataOutputStream dos = null;
        byte[] bytes = null;

        try {
            baos = new ByteArrayOutputStream();
            dos = new DataOutputStream(baos);
            dos.writeInt(x);
            dos.writeInt(y);
            dos.writeInt(dir.ordinal());
            dos.writeBoolean(moving);
            dos.writeInt(group.ordinal());
            dos.writeLong(id.getMostSignificantBits());
            dos.writeLong(id.getLeastSignificantBits());

            //清空缓冲区
            dos.flush();
            //转换为字节数组
            bytes = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return bytes;
    }

    public void parse(byte[] bytes) {
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));

        try {
            this.x = dis.readInt();
            this.y = dis.readInt();
            this.dir = Dir.values()[dis.readInt()];
            this.moving = dis.readBoolean();
            this.group = Group.values()[dis.read()];
            //高八位和 低八位
            this.id = new UUID(dis.readLong(), dis.readLong());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (dis != null) {
                    dis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public void handle() {
        //if this msg's id equals my tank's return

        //otherwise add new tank to my collection
        if(this.id.equals(TankFrame.INSTANCE.getGm().getMyTank().getId())){
            return;
        }

        if(TankFrame.INSTANCE.getGm().findTankByUUID(this.id)!=null){
            return;
        }

        Tank t = new Tank(this);

        TankFrame.INSTANCE.getGm().add(t);
    }
}
